<?php
/**
 * 文章分类管理
 * @author caoge
 * @version 2012-7-25
 */
class ArticleTypeAction extends AdminBaseAction{
	/**
	 * 分类列表
	 * 
	 * @author caoge
	 * @version 2012-7-25
	 */
	public function index(){
		$nowPage = $this->_get('p','',1);
		$ArticleType = D(ArticleType);
		$where = 'del_flag=0';
		import("ORG.Util.Page");
		$count = $ArticleType->where($where)->count();
		$p = new Page($count,10);
		
		$list = $ArticleType->relation(true)->where($where)
		->limit($p->firstRow.','.$p->listRows)->order('sort desc')->select();
		$this->assign('page',$p->show());
		$this->assign('nowPage',$nowPage);
		$this->assign('list',$list);
		$this->display();
	}
	
	/**
	 * 添加或者编辑类别
	 * 
	 * @author caoge
	 * @version 2012-7-26
	 */
	public function edit(){
		$nowPage = $this->_get('p','',1);
		$typeId = $this->_get('typeId','',0);
		$typeName = $this->_post('typeName','strip_tags','');
		$sort = $this->_post('sort','',0);
		if ($typeName != ''){
			$data = array(
					'type_name'=>$typeName,
					'sort'=>$sort
			);
			$ArticleType = D(ArticleType);
			if ($typeId == 0){
				//新增
				$ArticleType->data($data)->add();
				$this->success('添加成功',__APP__.'/ArticleType/index/');
			}else {
				$updateCondition['type_id'] = $typeId;
				$ArticleType->where($updateCondition)->data($data)->save();
				$this->success('编辑成功',__APP__.'/ArticleType/index/?&p='.$nowPage);
			}
		}else {
			if ($typeId == 0){
				$actionTile = '添加新类别';
			}else {
				$ArticleType = D(ArticleType);
				$condition['type_id'] = $typeId;
				$typeInfo = $ArticleType->where($condition)->find();
				if (!$typeInfo){
					$this->error('类别不存在',__APP__.'/ArticleType/index/');
				}
				$actionTile = '编辑类别';
				$this->assign('typeInfo',$typeInfo);
			}
			$this->assign('nowPage',$nowPage);
			$this->assign('actionTile',$actionTile);
			$this->display();
		}
	}
	
	/**
	 * 删除类别
	 * 
	 * @author caoge
	 * @version 2012-7-26
	 */
	public function delete(){
		$nowPage = $this->_get('p','',1);
		$typeId = $this->_get('typeId','',0);
		$ArticleType = D(ArticleType);
		$condition['type_id'] = $typeId;
		$data['del_flag'] = 1;
		$ArticleType->where($condition)->data($data)->save();
		$Article = D(Article); //将文章表里有这个类别的文章类别改成“默认分类”
		$Article->where(array('type_id'=>$typeId))->data(array('type_id'=>0))->save();
		$this->success('删除成功',__APP__.'/ArticleType/index/?&p='.$nowPage);
	}
	
	/**
	 * 异步查询被删除的类别下有没有文章
	 * 
	 * @author caoge
	 * @version 2012-7-26
	 */
	public function ajaxTypeDeleteCheck(){
		$typeId = $this->_get('typeId','',0);
		$arrayToJs = array('status'=>1,'msg'=>'确定删除吗？');
		if ($typeId != 0){
			$Article = D(Article);
			$condition['type_id'] = $typeId;
			$condition['del_flag'] = 0;
			$count = $Article->where($condition)->count();
			if ($count > 0){
				$arrayToJs['status'] = 0;
				$arrayToJs['msg'] = '此类别下有'.$count.'篇文章，将移到默认分类，确定删除吗？';
			}
		}
		echo json_encode($arrayToJs);
	}
}